package com.cat.service.impl;

import com.cat.dao.MemberMapper;
import com.cat.model.Member;
import com.cat.service.MemberService;
import com.cat.utils.EUDataGridResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * Created by lin on 17-9-7.
 */
@Service
public class MemberServiceImpl implements MemberService {
    private static final Logger logger = LoggerFactory.getLogger(MemberService.class);

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public int add(Member member) {
        if(logger.isDebugEnabled()){
            logger.debug("用户添加:[用户信息:{}]",member);
        }
        Date currentDate = new Date();
        member.setCreateTime(currentDate);
        member.setModifyTime(currentDate);
        int num = memberMapper.insert(member);
        return num;
    }

    @Override
    public int removeLogicalById(int id) {
        //TODO:数据库表没有删除标志字段，所以没有逻辑删除
        return 0;
    }

    @Override
    public int removePhysicalById(int id) {
        if(logger.isDebugEnabled()){
            logger.debug("用户删除:[id:{}]",id);
        }
        int num = memberMapper.deleteById(id);
        return num;
    }

    @Override
    public EUDataGridResult list(Member member, Integer page, Integer rows) {
        if(logger.isDebugEnabled()){
            logger.debug("用户列表查询:[搜索条件:{},page:{},rows:{}]",member,page,rows);
        }
        Page pageInfo = PageHelper.startPage(page,rows);
        List<Member> members = memberMapper.selectByObject(member);
        EUDataGridResult<Member> result =  new EUDataGridResult<>();
        result.setRows(members);
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @Override
    public Member getOne(Member member) {
        if(logger.isDebugEnabled()){
            logger.debug("用户查询:[搜索条件:{}]",member);
        }
        Member memberResult = memberMapper.selectOneByObject(member);
        return memberResult;
    }
}
